package com.school.service.impl;

import com.school.basic.repository.StudentInfoRepository;
import com.school.entity.*;
import com.school.repository.*;
import com.school.service.ArchivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArchivesServiceImp implements ArchivesService {
    @Autowired
    ArchivesRepository archivesRepository;
    @Autowired
    SelfStatementReportRepository selfStatementReportRepository;
    @Autowired
    StudentInfoRepository studentInfoRepository;
    @Autowired
    RealisticRecordRepository realisticRecordRepository;
    @Value("${web.upload.path}")
    String basePath;

    /**
     * 增加
     * @param archives
     * @return
     */
    @Override
    public Archives addArchives(Archives archives){
        archives.setCreateDate(new Date());
        archives.setUpdateDate(new Date());
        return archivesRepository.save(archives);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public Archives getDetailsById(String id) {
        Archives archives = archivesRepository.findById(Integer.valueOf(id)).get();
        /*SelfStatementReport selfStatementReport = selfStatementReportRepository.findById(Integer.valueOf(archives.getSelfAssessmentId())).get();
        if(selfStatementReport != null){
            archives.setSelfStatementReport(selfStatementReport);
        }
        TeacherComments teacherComments = teacherCommentsRepository.findById(Integer.valueOf(archives.getTeacherCommentId())).get();
        if(teacherComments != null){
            archives.setTeacherComments(teacherComments);
        }
        StudentInfo studentInfo = studentInfoRepository.findById(Integer.valueOf(archives.getStudentId())).get() ;
        if(studentInfo != null){
            archives.setStudentInfo(studentInfo);
        }
        String recordIdList = archives.getRecordId();
        String[] split = recordIdList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        List<RealisticRecord> realisticRecordList = realisticRecordRepository.findRealisticRecordsByIdIn(list);
        if(realisticRecordList.size() > 0){
            archives.setRealisticRecordList(realisticRecordList);
        }*/
        return archivesRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 根据条件分页查询
     * @param archives,page,size
     */
    @Override
    public Page<Archives> specificPageQueryArchivesList(Archives archives, int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC,"updateDate"));
        return archivesRepository.findAll(querySpecification,pageRequest);
    }

}
